﻿using System.Configuration;
using System.IO;

namespace OpenSoftStore.Client.Common
{
    /// <summary>
    /// 操作winform配置文件
    /// </summary>
    public static class SPAUserConfig
    {
        public static Configuration Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        /// <summary>
        /// 获得值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            var map = new ExeConfigurationFileMap { ExeConfigFilename = "User.config" };
            Config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (Config.AppSettings.Settings[key] == null) return "";
            return Config.AppSettings.Settings[key].Value;
        }

        /// <summary>
        /// 修改或增加值（保存值）
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static void SaveValue(string key, string value)
        {
            if (Config.AppSettings.Settings[key] == null)
            {
                Config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                Config.AppSettings.Settings[key].Value = value;
            }
            Config.Save(ConfigurationSaveMode.Modified);
        }

        /// <summary>
        /// 删除值
        /// </summary>
        /// <param name="key">key</param>
        public static void DeleteValue(string key)
        {
            Config.AppSettings.Settings.Remove(key);
        }

    }
}
